package com.global.gbinventory.data.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.global.gbinventory.data.model.Supplier;
import com.global.gbinventory.data.tablemodel.SupplierTableModel;
import com.global.gbinventory.data.view.SupplierListFrame;
import com.global.gbinventory.exception.EmptyTableException;
import com.global.gbinventory.exception.NoRowSelectedException;
import com.global.gbinventory.main.controller.MainController;

public class SupplierListController {
	
	private MainController main;
	private SupplierListFrame frame;
	private SupplierTableModel tableModel;
	private SupplierController supplierController;
	
	private int selectedRow;
	
	public SupplierListController(MainController main){
		this.main = main;
		this.tableModel = new SupplierTableModel(main.getDBMaster());
		selectedRow = -1;
	}
	
	public void start(boolean viewedOnly){
		initialize();
		frame.displayFrame(true, viewedOnly);
		main.addFrameToDesktopPane(frame);
		frame.moveToFront();
	}
	
	private void initialize(){
		this.frame = new SupplierListFrame();
		this.supplierController = main.getSupplierController();
		addListeners();
		frame.setTableModel(tableModel);
	}
	
	private void addListeners(){
		frame.addBtnOkListener(new BtnOkListener());
		frame.addBtnCancelListener(new BtnCancelListener());
	}
	
	public void setSelectedRow(int selectedRow){
		this.selectedRow = selectedRow;
	}
	
	public int getSelectedRow(){
		return selectedRow;
	}
	
	public void updateSupplier(String kode, String nama, String alamat, String kota, 
			String kodepos,String provinsi, String telepon, String namaagen, String hpagen, 
			String emailagen, String keterangan){
		
		tableModel.updateSupplier(selectedRow, kode, nama, alamat, kota, kodepos, provinsi, 
				telepon, namaagen, hpagen, emailagen, keterangan);
	
	}
	
	public void removeSupplier(){
		tableModel.removeSupplier(selectedRow);
	}
	
	public String isExistedKode(String kode){
		return tableModel.isExistedKode(kode);
	}

	class BtnOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try{
				selectedRow = frame.getTableSelectedRow();
				if(tableModel.isEmptyTable()){
					throw new EmptyTableException();
				}
				
				if(selectedRow < 0){
					throw new NoRowSelectedException();
				}
				
				Supplier supplier = tableModel.getSupplier(selectedRow);
				String kode = supplier.getKode();
				String nama = supplier.getNama();
				String alamat = supplier.getAlamat();
				String kota = supplier.getKota();
				String kodepos = supplier.getKodepos();
				String provinsi = supplier.getProvinsi();
				String telepon = supplier.getTelepon();
				String namaagen = supplier.getNamaagen();
				String hpagen = supplier.getHpagen();
				String emailagen = supplier.getEmailagen();
				String keterangan = supplier.getKeterangan();
				
				supplierController.fillData(kode, nama, alamat, kota, kodepos, provinsi, 
						telepon, namaagen, hpagen, emailagen, keterangan);
				frame.disposeFrame();
				main.removeFrameFromDesktopPane(frame);
				
				supplierController.activateEditDelete();
				
			} catch(NoRowSelectedException e1){
				e1.printError(frame);
				
			} catch(EmptyTableException e2){
				e2.printError(frame);
			}
			
		}
		
	}
	
	class BtnCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			frame.disposeFrame();
			main.removeFrameFromDesktopPane(frame);
		}
		
	}
	
}
